package my.jwds.p1706;

import java.util.Arrays;

public class FindBall {

    public static void main(String[] args) {
        FindBall obj = new FindBall();
        int[][]grid = {
                {1}
        };
        System.out.println(Arrays.toString(obj.findBall(grid)));
    }

    public int[] findBall(int[][] grid) {
        int m = grid.length,n = grid[0].length;
        int[]dp = new int[n];
        for (int i = 0; i < n; i++) {
            dp[i] = i;
        }
        for (int i = m-1; i >= 0; i--) {
            int [] temp = new int[n];
            for (int j = 0; j < n; j++) {
                temp[j] = -1;
                if (grid[i][j] == -1 && j != 0 && grid[i][j] == grid[i][j-1] || grid[i][j] == 1 && j!=n-1 && grid[i][j] == grid[i][j+1] ){
                    temp[j] = dp[j+ grid[i][j]];
                }
            }
            dp = temp;
        }
        return dp;
    }
}